package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.lin.mapper.MDesignProcedureMapper;
import com.lin.myenum.NumberType;
import com.lin.pojo.DFile;
import com.lin.pojo.MDesignProcedure;
import com.lin.pojo.MDesignProcedureDetails;
import com.lin.pojo.PageVo;
import com.lin.service.DFileService;
import com.lin.service.MDesignProcedureDetailsService;
import com.lin.service.MDesignProcedureService;
import com.lin.utils.CreateNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MDesignProcedureServiceImpl extends ServiceImpl<MDesignProcedureMapper,MDesignProcedure> implements MDesignProcedureService {

    @Autowired
    MDesignProcedureDetailsService mDesignProcedureDetailsService;

    @Autowired
    CreateNumber createNumber;

    @Autowired
    DFileService dFileService;

    @Override
    public PageVo<MDesignProcedure> queryallbycond(MDesignProcedure mDesignProcedure, int pageno, int pagesize) {
        Page<MDesignProcedure> page=new Page<MDesignProcedure>(pageno,pagesize);
        QueryWrapper<MDesignProcedure> queryWrapper=new QueryWrapper<MDesignProcedure>();
        //审核判断
        if(StringUtils.isNotBlank(mDesignProcedure.getCheckTag())){
            queryWrapper.eq("CHECK_TAG",mDesignProcedure.getCheckTag());
        }
        Page<MDesignProcedure> page1 = this.page(page,queryWrapper);


        PageVo<MDesignProcedure> pageVo = new PageVo<MDesignProcedure>();
        pageVo.setTotal(page1.getTotal());
        pageVo.setRows(page1.getRecords());

        return pageVo;

    }

    @Override
    public Map addMDesignProcedure(MDesignProcedure mDesignProcedure) {
        //获取工序设计编号
        String designid = createNumber.createNum(NumberType.designId);
        //添加物料设计表 页面没给的数据
        // 后台封装
        mDesignProcedure.setDesignId(designid);  //设置物料设计编号
        mDesignProcedure.setCheckTag("S001-0"); //审核标志 等待审核
        mDesignProcedure.setRegisterTime(new Date()); //注册时间
        mDesignProcedure.setDesignModuleTag("G002-0");//工序物料设计标志 未设计
        mDesignProcedure.setDesignModuleChangeTag("G003-0");//工序物料变更标志 未变更
        mDesignProcedure.setChangeTag("B002-0"); //变更标志 未变更
        //工时总成本   明细小计  累加求和 算出来
        double costpricesum=0;


        //工序组成明细封装 页面没给的数据 后台封装
        for (int i = 0; i < mDesignProcedure.getDetails().size(); i++) {
            MDesignProcedureDetails details = mDesignProcedure.getDetails().get(i);
            details.setDetailsNumber(i+1);
            details.setDesignModuleTag("D002-0");  //当前工序物料标志 未设计
            details.setDesignModuleChangeTag("D003-0");  //当前工序物料变更标志 未变更
            //后端根据数量和价钱再次计算	工时成本小计
            details.setSubtotal(details.getLabourHourAmount()*details.getCostPrice());

            costpricesum+=details.getSubtotal();

        }

        mDesignProcedure.setCostPriceSum(costpricesum);


        boolean num = this.save(mDesignProcedure);

        for (MDesignProcedureDetails detail : mDesignProcedure.getDetails()) {
            //父级序号
            detail.setParentId(mDesignProcedure.getId());
        }

        //添加物料设计明细表
        boolean b = mDesignProcedureDetailsService.saveBatch(mDesignProcedure.getDetails());


        //修改产品为已设计状态
        DFile dFile =new DFile();
        dFile.setDesignProcedureTag("G001-1");
        //档案对象 修改条件组装
        UpdateWrapper<DFile> updateWrapper=new UpdateWrapper<DFile>();
        System.out.println(mDesignProcedure.getProductId());
        updateWrapper.eq("PRODUCT_ID",mDesignProcedure.getProductId());

        boolean update = dFileService.update(dFile, updateWrapper);


        Map map = new HashMap();
        map.put("code", "0");
        map.put("msg", "设计失败");
        if (num && b && update) {
            map.put("code", "1");
            map.put("msg", "设计成功");
        }
        return map;
    }

    @Override
    public Map shdesign(MDesignProcedure mDesignProcedure) {
        mDesignProcedure.setCheckTime(new Date());

        //工时总成本   明细小计  累加求和 算出来
        double costpricesum=0;


        //工序组成明细封装 页面没给的数据 后台封装
        for (int i = 0; i < mDesignProcedure.getDetails().size(); i++) {
            MDesignProcedureDetails details = mDesignProcedure.getDetails().get(i);
            //后端根据页面传递的新数据数量和价钱再次计算	工时成本小计
            details.setSubtotal(details.getLabourHourAmount()*details.getCostPrice());

            costpricesum+=details.getSubtotal();

        }

        mDesignProcedure.setCostPriceSum(costpricesum);


        UpdateWrapper<MDesignProcedure> updateWrapper =new UpdateWrapper<MDesignProcedure>();
        updateWrapper.eq("DESIGN_ID",mDesignProcedure.getDesignId());

        boolean num = this.update(mDesignProcedure,updateWrapper);

        //批量根据主键id 修改物料设计明细表
        boolean b = mDesignProcedureDetailsService.updateBatchById(mDesignProcedure.getDetails());


        Map map = new HashMap();
        map.put("code", "0");
        map.put("msg", "审核失败");
        if (num && b ) {
            map.put("code", "1");
            map.put("msg", "审核成功");
        }
        return map;
    }

    @Override
    public Map upMDesignProcedure(MDesignProcedure mDesignProcedure) {
        //获取工序设计编号
        String designid = createNumber.createNum(NumberType.designId);
        //添加物料设计表 页面没给的数据
        // 后台封装
        mDesignProcedure.setDesignId(designid);  //设置物料设计编号
        mDesignProcedure.setCheckTag("S001-0"); //审核标志 等待审核
        mDesignProcedure.setRegisterTime(new Date()); //注册时间
        mDesignProcedure.setDesignModuleTag("G002-0");//工序物料设计标志 未设计
        mDesignProcedure.setDesignModuleChangeTag("G003-0");//工序物料变更标志 未变更
        mDesignProcedure.setChangeTag("B002-0"); //变更标志 未变更
        //工时总成本   明细小计  累加求和 算出来
        double costpricesum=0;


        //工序组成明细封装 页面没给的数据 后台封装
        for (int i = 0; i < mDesignProcedure.getDetails().size(); i++) {
            MDesignProcedureDetails details = mDesignProcedure.getDetails().get(i);
            details.setDetailsNumber(i+1);
            details.setDesignModuleTag("D002-0");  //当前工序物料标志 未设计
            details.setDesignModuleChangeTag("D003-0");  //当前工序物料变更标志 未变更
            //后端根据数量和价钱再次计算	工时成本小计
            details.setSubtotal(details.getLabourHourAmount()*details.getCostPrice());

            costpricesum+=details.getSubtotal();

        }

        mDesignProcedure.setCostPriceSum(costpricesum);


        boolean num = this.save(mDesignProcedure);

        for (MDesignProcedureDetails detail : mDesignProcedure.getDetails()) {
            //父级序号
            detail.setParentId(mDesignProcedure.getId());
        }

        //添加物料设计明细表
        boolean b = mDesignProcedureDetailsService.saveBatch(mDesignProcedure.getDetails());


        //修改产品为已设计状态
        DFile dFile =new DFile();
        dFile.setDesignProcedureTag("G001-1");
        //档案对象 修改条件组装
        UpdateWrapper<DFile> updateWrapper=new UpdateWrapper<DFile>();
        System.out.println(mDesignProcedure.getProductId());
        updateWrapper.eq("PRODUCT_ID",mDesignProcedure.getProductId());

        boolean update = dFileService.update(dFile, updateWrapper);


        Map map = new HashMap();
        map.put("code", "0");
        map.put("msg", "变更失败");
        if (num && b && update) {
            map.put("code", "1");
            map.put("msg", "变更成功");
        }
        return map;
    }



}
